package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.PayrollDto;
import xyz.zhouzhaodong.crm.entity.Payroll;
import xyz.zhouzhaodong.crm.mapper.PayrollMapper;
import xyz.zhouzhaodong.crm.service.IPayrollService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 薪资表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-21
 */
@Service
public class PayrollServiceImpl extends ServiceImpl<PayrollMapper, Payroll> implements IPayrollService {

    @Resource
    PayrollMapper payrollMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addPayroll(Payroll payroll) {
        try {
            // 新增
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            payroll.setId(id);
            payroll.setCreateUser(payroll.getStaffId());
            payroll.setCreateTime(new Date());
            payrollMapper.insert(payroll);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deletePayroll(Payroll payroll) {
        try {
            Payroll p = new Payroll();
            p.setId(payroll.getId());
            p.setFlag(1);
            p.setUpdateUser(payroll.getStaffId());
            p.setUpdateTime(new Date());
            payrollMapper.updateById(p);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updatePayroll(Payroll payroll) {
        try {
            payroll.setUpdateUser(payroll.getStaffId());
            payroll.setUpdateTime(new Date());
            // 不更新staffId
            payroll.setStaffId(null);
            payrollMapper.updateById(payroll);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectPayroll(PayrollDto payrollDto) {
        Page<PayrollDto> page = new Page<>(payrollDto.getPageNo(),
                payrollDto.getPageSize() == 0 ? 10 : payrollDto.getPageSize());
        if(payrollDto.getFlag() == null){
            payrollDto.setFlag(0);
        }
        String type = payrollDto.getType();
        if(type != null && !"".equals(type)){
            StringBuilder stringBuilder = new StringBuilder("(");
            String[] str = type.split(",");
            for(String s : str){
                stringBuilder.append("'").append(s).append("',");
            }
            String all = stringBuilder.substring(0, stringBuilder.length() - 1) + ")";
            payrollDto.setType(all);
        }
        IPage<PayrollDto> list = payrollMapper.selectPayroll(payrollDto, page);
        return RestMessage.success(list);
    }
}
